<?php
require_once "BaseSQLDAO.php";
require_once dirname(__FILE__)."/../ContactDAO.php";
require_once "SQLStatements.php";
require_once "ErrorMessages.php";
require_once dirname(__FILE__)."/../DataAccessException.php";
require_once dirname(__FILE__)."/../../model/Contact.php";

class ContactSQLDAOImpl extends BaseSQLDAO implements ContactDAO {

    public function create($object) {
        $stmt = $this->getConnection()->prepare(SQLStatements::CONTACT_INSERT);
        $stmt->bind_param("issss", $object->getId(), $object->getOwnerId(), $object->getUid(), $object->getFullName(), $object->getMobileNumber());
        $stmt->execute();
        if ($stmt->affected_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_CREATE_ERROR);
        }
    }

    public function read($id) {
        $contact = new Contact();
        $stmt = $this->getConnection()->prepare(SQLStatements::CONTACT_SELECT_BY_ID);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->bind_result($userId, $ownerId, $userUid, $fullName, $mobileNumber);
        $stmt->store_result();
        $stmt->fetch();
        if ($stmt->num_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_READ_ERROR);
        }
        $contact->setId($userId);
        $contact->setUid($userUid);
        $contact->setOwnerId($ownerId);
        $contact->setFullName($fullName);
        $contact->setMobileNumber($mobileNumber);

        return $contact;
    }

    public function getContactsByUserId($id) {
        $contactArray = array();

        $stmt = $this->getConnection()->prepare(SQLStatements::CONTACT_SELECT_MANY_BY_OWNER_ID);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $stmt->bind_result($userId, $ownerId, $userUid, $fullName, $mobileNumber);
        $stmt->store_result();
        while ($stmt->fetch()) {
            $contact = new Contact();
            $contact->setId($userId);
            $contact->setUid($userUid);
            $contact->setOwnerId($ownerId);
            $contact->setFullName($fullName);
            $contact->setMobileNumber($mobileNumber);
            $contactArray[] = $contact;
        }


        return $contactArray;
    }

    public function update($object) {
        $stmt = $this->getConnection()->prepare(SQLStatements::CONTACT_UPDATE);
        $stmt->bind_param("ssssi", $object->getOwnerId(), $object->getUid(), $object->getFullName(), $object->getMobileNumber(), $object->getId());
        $stmt->execute();
        if ($stmt->affected_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_UPDATE_ERROR);
        }
    }

    public function delete($id) {
        $stmt = $this->getConnection()->prepare(SQLStatements::CONTACT_DELETE_BY_ID);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        if ($stmt->affected_rows != 1) {
            throw new DataAccessException(ErrorMessages::OBJECT_DELETE_ERROR);
        }
    }

    public function deleteAll() {
        $stmt = $this->getConnection()->prepare("DELETE FROM contact");
        $stmt->execute();
    }

    public function count() {
        $stmt = $this->getConnection()->prepare("SELECT COUNT(contact_id) FROM contact");
        $stmt->execute();
        $stmt->bind_result($count);
        $stmt->fetch();
        return $count;
    }

}
?>
